/**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2016-2026 hmc,Inc.All Rights Reserved.
 */
package com.sys.midware.config.utils;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
 * 
 * <b>ClassName：</b> ConfigFileUtil <br/>
 * <b>Description：</b> xml工具类，用来加载，读取、处理xml文件和节点 <br/>
 * <b>@author：</b> mobing <br/>
 * <b>@date：</b> 2015年10月16日 下午3:45:02 <br/>
 * <b>@version: </b>  <br/>
 */
public class XmlUtil {
	
	/**
	 * 在某个节点下，根据节点名称获取该节点的值。类似于key-value形式
	 * @param element
	 * @param nodeName
	 * @return
	 */
	public static String getNodeValueString (Element element,String nodeName){
		String nodeValStr=null;
		NodeList children = element.getChildNodes();
		for (int i = 0, size = children.getLength(); i < size; i++) {
			Node childNode = children.item(i);
			if (childNode instanceof Element) {
				Element child = (Element) childNode;
				String childNodeName = child.getNodeName();
				String nodeValue = child.getTextContent();
				if (nodeName.equals(childNodeName)) {
					nodeValStr = nodeValue.trim();
				} 
				continue;
			}
		}
		return nodeValStr;
	}
	/**
	 * 获取integer类型的节点值
	 * @param element
	 * @param nodeName
	 * @return
	 */
	public static Integer getNodeValueInteger (Element element,String nodeName){
		String nodeValueStr =getNodeValueString(element,nodeName);
		if ( nodeValueStr==null){
			return null;
		}
		
		return Integer.valueOf(nodeValueStr);
	}
	/**
	 * 获取boolean类型的节点值
	 * @param element
	 * @param nodeName
	 * @return
	 */
	public static Boolean getNodeValueBoolean (Element element,String nodeName){
		String  nodeValueStr=getNodeValueString(element,nodeName);
		if ( nodeValueStr==null){
			return null;
		}
		return Boolean.valueOf(nodeValueStr);
	}
	
	/**
	 * 在某个节点下，根据节点名称获取该节点的值。类似于key-value形式
	 * @param element
	 * @param nodeName
	 * @return
	 */
	public static List<String> getNodeValueList (Element element,String nodeName){
		List<String> nodeValueList = new ArrayList<String>();
		String nodeValStr=null;
		NodeList children = element.getChildNodes();
		for (int i = 0, size = children.getLength(); i < size; i++) {
			Node childNode = children.item(i);
			if (childNode instanceof Element) {
				Element child = (Element) childNode;
				String childNodeName = child.getNodeName();
				String nodeValue = child.getTextContent();
				if (nodeName.equals(childNodeName)) {
					nodeValStr = nodeValue.trim();
					nodeValueList.add(nodeValStr);
				} 
				continue;
			}
		}
		return nodeValueList;
	}

}
